package com.peking.donations.base.security;

import com.google.common.collect.Lists;
import com.peking.donations.model.DonationUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;

import java.util.Collection;
import java.util.Date;
import java.util.List;

public class LoggedUser extends User {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8414926367492342559L;

	private int userId;
	private String openId;
	private String username;
	private String fullname;
	private String nickName;
	private String headImg;
	private String grade;
	private String department;
	private int type;
	private Date lastLogin;


	public LoggedUser(String userName, Collection<? extends GrantedAuthority> authorities){
		super(userName, null, convert2Authorities());

	}

	public LoggedUser(String userName, String password, DonationUser sysUser){
		super(userName, password, convert2Authorities());
		if(sysUser!=null) {
			this.userId = sysUser.getId();
			this.openId = sysUser.getWeixinOpenId();
			this.nickName = sysUser.getWeixinNickname();
			this.headImg = sysUser.getWeixinHeadImg();
			this.fullname = sysUser.getRealName();
			this.grade = sysUser.getGrade();
			this.department = sysUser.getDepartment();
			this.type = sysUser.getType();
		}

	}

	private static List<GrantedAuthority> convert2Authorities() {
		List<GrantedAuthority> authorities = Lists.newArrayList();
		// default role
		authorities.add(new SimpleGrantedAuthority("ROLE_USER"));

		return authorities;
	}
	
	public static LoggedUser anonymous(String openId,String nickName,String headImg) {
        LoggedUser user = new LoggedUser("ANONYMOUS", "ANONYMOUS", null);
        user.openId = openId;
        user.userId = -1;
        user.username = "ANONYMOUS";
        user.fullname = "ANONYMOUS";
        user.nickName = nickName;
        user.headImg = headImg;
        return user;
	}
	

	public boolean isAnonymous() {
		return this.username.equals("ANONYMOUS");
	}

	public void addAuthority(String role){
		SimpleGrantedAuthority authority=new SimpleGrantedAuthority(role);
		this.getAuthorities().add(authority);
	}
	public String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getFullname() {
		return fullname;
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public String getHeadImg() {
		return headImg;
	}

	public void setHeadImg(String headImg) {
		this.headImg = headImg;
	}

	public Date getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	@Override
	public String toString() {
		return "LoggedUser [userId=" + userId + ", openId=" + openId
				+ ", username=" + username + ", fullname=" + fullname
				+ ", nickName=" + nickName + ", headImg=" + headImg
				+ ", lastLogin=" + lastLogin + "]";
	}
}
